package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class DamageSpecialPane extends AdjustablePanel {
    private JCheckBox cbTotal;
    private JCheckBox cbCritical;
    private JCheckBox cbLifeLeech;
    private JCheckBox cbReflect;
    private JCheckBox cbProjectilePierce;
    private JCheckBox cbAttackSpeed;
    private JCheckBox cbTotalSpeed;
    private JCheckBox cbDefenseAbil;
    private JCheckBox cbOffenseAbil;
    private JCheckBox cbResRedAbsolute;
    private JCheckBox cbResRedPercent;
    private JCheckBox cbRetalReflect;
    private JCheckBox cbManaCostRed;
    private JCheckBox cbPetBonus;

    public DamageSpecialPane() {
        this(1);
    }

    public DamageSpecialPane(final int direction) {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbTotal).addComponent(this.cbCritical).addComponent(this.cbLifeLeech).addComponent(this.cbReflect).addComponent(this.cbProjectilePierce).addComponent(this.cbAttackSpeed).addComponent(this.cbTotalSpeed).addComponent(this.cbDefenseAbil).addComponent(this.cbOffenseAbil).addComponent(this.cbResRedAbsolute).addComponent(this.cbResRedPercent).addComponent(this.cbRetalReflect).addComponent(this.cbManaCostRed).addComponent(this.cbPetBonus));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbTotal)).addGroup(layout.createParallelGroup().addComponent(this.cbCritical)).addGroup(layout.createParallelGroup().addComponent(this.cbLifeLeech)).addGroup(layout.createParallelGroup().addComponent(this.cbReflect)).addGroup(layout.createParallelGroup().addComponent(this.cbProjectilePierce)).addGroup(layout.createParallelGroup().addComponent(this.cbAttackSpeed)).addGroup(layout.createParallelGroup().addComponent(this.cbTotalSpeed)).addGroup(layout.createParallelGroup().addComponent(this.cbDefenseAbil)).addGroup(layout.createParallelGroup().addComponent(this.cbOffenseAbil)).addGroup(layout.createParallelGroup().addComponent(this.cbResRedAbsolute)).addGroup(layout.createParallelGroup().addComponent(this.cbResRedPercent)).addGroup(layout.createParallelGroup().addComponent(this.cbRetalReflect)).addGroup(layout.createParallelGroup().addComponent(this.cbManaCostRed)).addGroup(layout.createParallelGroup().addComponent(this.cbPetBonus));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbTotal, this.cbLifeLeech);
        layout.linkSize(0, this.cbTotal, this.cbLifeLeech);
        layout.linkSize(0, this.cbTotal, this.cbReflect);
        layout.linkSize(0, this.cbTotal, this.cbProjectilePierce);
        layout.linkSize(0, this.cbTotal, this.cbAttackSpeed);
        layout.linkSize(0, this.cbTotal, this.cbTotalSpeed);
        layout.linkSize(0, this.cbTotal, this.cbDefenseAbil);
        layout.linkSize(0, this.cbTotal, this.cbOffenseAbil);
        layout.linkSize(0, this.cbTotal, this.cbOffenseAbil);
        layout.linkSize(0, this.cbTotal, this.cbResRedPercent);
        layout.linkSize(0, this.cbTotal, this.cbRetalReflect);
        layout.linkSize(0, this.cbTotal, this.cbManaCostRed);
        layout.linkSize(0, this.cbTotal, this.cbPetBonus);
        layout.linkSize(1, this.cbTotal, this.cbCritical);
        layout.linkSize(1, this.cbTotal, this.cbLifeLeech);
        layout.linkSize(1, this.cbTotal, this.cbReflect);
        layout.linkSize(1, this.cbTotal, this.cbProjectilePierce);
        layout.linkSize(1, this.cbTotal, this.cbAttackSpeed);
        layout.linkSize(1, this.cbTotal, this.cbTotalSpeed);
        layout.linkSize(1, this.cbTotal, this.cbDefenseAbil);
        layout.linkSize(1, this.cbTotal, this.cbOffenseAbil);
        layout.linkSize(1, this.cbTotal, this.cbResRedAbsolute);
        layout.linkSize(1, this.cbTotal, this.cbResRedPercent);
        layout.linkSize(1, this.cbTotal, this.cbRetalReflect);
        layout.linkSize(1, this.cbTotal, this.cbManaCostRed);
        layout.linkSize(1, this.cbTotal, this.cbPetBonus);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_DMG_SPECIAL"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbTotal == null) {
            this.cbTotal = new JCheckBox();
        }
        this.cbTotal.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_TOTAL"));
        this.cbTotal.setFont(fntCheck);
        if (this.cbCritical == null) {
            this.cbCritical = new JCheckBox();
        }
        this.cbCritical.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_CRITICAL"));
        this.cbCritical.setFont(fntCheck);
        if (this.cbLifeLeech == null) {
            this.cbLifeLeech = new JCheckBox();
        }
        this.cbLifeLeech.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_LIFELEECH"));
        this.cbLifeLeech.setFont(fntCheck);
        if (this.cbReflect == null) {
            this.cbReflect = new JCheckBox();
        }
        this.cbReflect.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_REFLECT"));
        this.cbReflect.setFont(fntCheck);
        if (this.cbProjectilePierce == null) {
            this.cbProjectilePierce = new JCheckBox();
        }
        this.cbProjectilePierce.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_PROJECTILE_PIERCE"));
        this.cbProjectilePierce.setFont(fntCheck);
        if (this.cbAttackSpeed == null) {
            this.cbAttackSpeed = new JCheckBox();
        }
        this.cbAttackSpeed.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_SLOW_ATTACK"));
        this.cbAttackSpeed.setFont(fntCheck);
        if (this.cbTotalSpeed == null) {
            this.cbTotalSpeed = new JCheckBox();
        }
        this.cbTotalSpeed.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_SLOW_TOTAL"));
        this.cbTotalSpeed.setFont(fntCheck);
        if (this.cbDefenseAbil == null) {
            this.cbDefenseAbil = new JCheckBox();
        }
        this.cbDefenseAbil.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_SLOW_DA"));
        this.cbDefenseAbil.setFont(fntCheck);
        if (this.cbOffenseAbil == null) {
            this.cbOffenseAbil = new JCheckBox();
        }
        this.cbOffenseAbil.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_SLOW_OA"));
        this.cbOffenseAbil.setFont(fntCheck);
        if (this.cbResRedAbsolute == null) {
            this.cbResRedAbsolute = new JCheckBox();
        }
        this.cbResRedAbsolute.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RES_RED_ABS"));
        this.cbResRedAbsolute.setFont(fntCheck);
        if (this.cbResRedPercent == null) {
            this.cbResRedPercent = new JCheckBox();
        }
        this.cbResRedPercent.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RES_RED_PERC"));
        this.cbResRedPercent.setFont(fntCheck);
        if (this.cbRetalReflect == null) {
            this.cbRetalReflect = new JCheckBox();
        }
        this.cbRetalReflect.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RETAL_REFLECT"));
        this.cbRetalReflect.setFont(fntCheck);
        if (this.cbManaCostRed == null) {
            this.cbManaCostRed = new JCheckBox();
        }
        this.cbManaCostRed.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "STAT_MANACOST_REDUCTION"));
        this.cbManaCostRed.setFont(fntCheck);
        if (this.cbPetBonus == null) {
            this.cbPetBonus = new JCheckBox();
        }
        this.cbPetBonus.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_PET_BONUS"));
        this.cbPetBonus.setFont(fntCheck);
    }

    public void clear() {
        this.cbTotal.setSelected(false);
        this.cbCritical.setSelected(false);
        this.cbLifeLeech.setSelected(false);
        this.cbReflect.setSelected(false);
        this.cbAttackSpeed.setSelected(false);
        this.cbTotalSpeed.setSelected(false);
        this.cbDefenseAbil.setSelected(false);
        this.cbOffenseAbil.setSelected(false);
        this.cbResRedAbsolute.setSelected(false);
        this.cbResRedPercent.setSelected(false);
        this.cbRetalReflect.setSelected(false);
        this.cbManaCostRed.setSelected(false);
        this.cbPetBonus.setSelected(false);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        if (this.cbTotal.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 501);
        }
        if (this.cbCritical.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 502);
        }
        if (this.cbLifeLeech.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 503);
        }
        if (this.cbReflect.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 504);
        }
        if (this.cbProjectilePierce.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 509);
        }
        if (this.cbAttackSpeed.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 505);
        }
        if (this.cbTotalSpeed.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 506);
        }
        if (this.cbDefenseAbil.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 507);
        }
        if (this.cbOffenseAbil.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 508);
        }
        if (this.cbResRedAbsolute.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 510);
            SelectionCriteria.addStatInfos(criteria, 512);
            SelectionCriteria.addStatInfos(criteria, 514);
        }
        if (this.cbResRedPercent.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 511);
            SelectionCriteria.addStatInfos(criteria, 513);
            SelectionCriteria.addStatInfos(criteria, 515);
        }
        if (this.cbRetalReflect.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 516);
        }
        if (this.cbManaCostRed.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 517);
        }
        criteria.petBonus = this.cbPetBonus.isSelected();
    }
}
